package com.game.centerserver.infrastructure.service.impl;

import com.game.centerserver.infrastructure.entity.Product;
import com.game.centerserver.infrastructure.dao.ProductDao;
import com.game.centerserver.infrastructure.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Product)表服务实现类
 *
 * @author makejava
 * @since 2024-11-19 15:52:30
 */
@Service("productService")
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductDao productDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Product queryById(Integer id) {
        return this.productDao.queryById(id);
    }

    @Override
    public List<Product> queryAll() {
        return productDao.queryAll();
    }

    @Override
    public List<Product> queryByCondition() {
        return productDao.queryByCondition();
    }

    /**
     * 新增数据
     *
     * @param product 实例对象
     * @return 实例对象
     */
    @Override
    public Product insert(Product product) {
        if (product.getProductInfo() == null ||
                product.getProductName() == null ||
                product.getMemSize() == null ||
                product.getCpuFrequency() == null ||
                product.getDownstreamBandwidth() == null ||
                product.getUpstreamBandwidth() == null ||
                product.getCpuCores() == null
        ) {
            return null;
        }

        if (product.getShard() == null) {product.setShard(1);}

        int line = this.productDao.insert(product);
        if (line <= 0) {return null;}
        return product;
    }

    /**
     * 修改数据
     *
     * @param product 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Product product) {

        return this.productDao.update(product) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.productDao.deleteById(id) > 0;
    }
}
